package com.zs.backend.modules.mobileManagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.backend.common.api.CommonResult;
import com.zs.backend.modules.mobileManagement.service.MobileManagementService;
import com.zs.backend.modules.userQueryPwd.model.UserQueryPwd;
import com.zs.backend.modules.userQueryPwd.service.UserQueryPwdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "查看密码功能")
@RequestMapping("/userQueryPwd")
public class UserQueryPwdController {

    @Autowired
    private UserQueryPwdService userQueryPwdService;

    @Autowired
    private MobileManagementService managementService;

    @ApiOperation("验证查看密码")
    @GetMapping("/checkPwd/{pwd}/{type}")
    public CommonResult checkPwd(
        @ApiParam(value = "密码", name = "pwd")
        @PathVariable String pwd,
        @ApiParam(value = "类型（1：手机列查询密码;2:注册类证书；3：非注册类证书4：企业部订单管理）", name = "type")
        @PathVariable Integer type){
        boolean result = managementService.checkPwd(pwd, type);
        return CommonResult.success(result);
    }

    @ApiOperation("设置查看密码")
    @GetMapping("/setPwd/{pwd}/{type}")
    public CommonResult setPwd(
        @ApiParam(value = "密码", name = "pwd")
        @PathVariable String pwd,
        @ApiParam(value = "类型（1：手机列查询密码;2:注册类证书；3：非注册类证书4：企业部订单管理）", name = "type")
        @PathVariable Integer type){
        QueryWrapper<UserQueryPwd> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserQueryPwd> lambda = wrapper.lambda();
        lambda.eq(UserQueryPwd::getType, type);

        UserQueryPwd userQueryPwd = userQueryPwdService.getOne(wrapper);
        if(userQueryPwd != null){
            userQueryPwd.setPwd(pwd);
            userQueryPwdService.updateById(userQueryPwd);
            return CommonResult.success(true);
        }

        boolean result = managementService.setPwd(pwd, type);
        return CommonResult.success(result);
    }

}

